import pymysql
from typing import Dict, Any

class DatabaseConfig:
    @staticmethod
    def get_connection_params() -> Dict[str, Any]:
        """获取数据库连接参数，统一校对规则"""
        return {
            'host': 'localhost',
            'port': 3306,
            'user': 'root',
            'password': '123456',
            'database': 'fastapi',
            'charset': 'utf8mb4',
            'collation': 'utf8mb4_0900_ai_ci',
            'autocommit': True,
            'init_command': "SET NAMES utf8mb4 COLLATE utf8mb4_0900_ai_ci"
        }
    
    @staticmethod
    def create_connection():
        """创建数据库连接"""
        params = DatabaseConfig.get_connection_params()
        return pymysql.connect(**params)
    
    @staticmethod
    def get_connection_string() -> str:
        """获取数据库连接字符串"""
        params = DatabaseConfig.get_connection_params()
        return (f"mysql+pymysql://{params['user']}:{params['password']}@"
                f"{params['host']}:{params['port']}/{params['database']}"
                f"?charset=utf8mb4&collation=utf8mb4_unicode_ci") 